package com.crm.dao;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.crm.entity.Opportunity;
import com.crm.entity.Quotation;
import com.crm.util.ApplicationContext;
import com.crm.util.Constants;


public class PendingQuotationsDAO {
	protected final Log logger = LogFactory.getLog(getClass());
	
	public  List<Opportunity> pendingQuotations(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			 List<Opportunity> objs=null;
			Query query=session.createQuery("from  Opportunity where business_id=:busid and status in('"+Constants.ST_SOURCE_INFO_RECEIVED+"','"+Constants.ST_SOURCE_INFO_SENT+"','"+Constants.ST_SOURCE_INFO_PAR_IMPORT+"')");
			query.setInteger("busid", busid);
			Object ob=query.list();
			objs=(List<Opportunity>)ob;
			return objs;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  Quotation getRemarksByEnqid(int id,int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			Query query=session.createQuery("from Quotation where enqid=:id and business_id=:busid");
			query.setInteger("id", id);
			query.setInteger("busid", busid);
			Object ob=query.list();
			Quotation q=null;
			List<Quotation> lq=(List<Quotation>)ob;
			Iterator it=lq.iterator();
			while(it.hasNext()==true){
				Object o=it.next();
				q=(Quotation)o;
			}
			return q;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}

	public  void updateRemarks(int id,String rem,int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			Query query=session.createQuery("update Quotation set remarks=:rem where id=:id and business_id=:busid");
			query.setInteger("id", id);
			query.setInteger("busid", busid);
			query.setString("rem", rem);
			query.executeUpdate();
			session.getTransaction().commit();
			return;
		}
		catch (Exception e) {
		if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
}
